package com.ruoyi.controller;


import com.ruoyi.domain.Banner;
import com.ruoyi.platform.annotation.Anonymous;
import com.ruoyi.platform.annotation.Log;
import com.ruoyi.platform.core.controller.BaseController;
import com.ruoyi.platform.core.domain.AjaxResult;
import com.ruoyi.platform.core.page.TableDataInfo;
import com.ruoyi.platform.enums.BusinessType;
import com.ruoyi.platform.utils.poi.ExcelUtil;
import com.ruoyi.service.IBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 轮播图Controller
 *
 * @author tp
 * @date 2022-11-01
 */
@RestController
@RequestMapping("/admin/banner")
@Api(tags = {"轮播图服务"})
public class BannerController extends BaseController {

    @Autowired
    private IBannerService bannerService;

    /**
     * 查询轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('admin:banner:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询轮播图列表", httpMethod = "GET", notes = "查询轮播图列表")
    public TableDataInfo list(Banner banner) {
        startPage();
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('admin:banner:export')")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出轮播图列表", httpMethod = "POST", notes = "导出轮播图列表")
    public void export(HttpServletResponse response, Banner banner) {
        List<Banner> list = bannerService.selectBannerList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<>(Banner.class);
        util.exportExcel(response, list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:banner:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取轮播图详细信息", httpMethod = "GET", notes = "获取轮播图详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(bannerService.selectBannerById(id));
    }

    /**
     * 新增轮播图
     */
    @PreAuthorize("@ss.hasPermi('admin:banner:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增轮播图", httpMethod = "POST", notes = "新增轮播图")
    public AjaxResult add(@RequestBody Banner banner) {
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改轮播图
     */
    @PreAuthorize("@ss.hasPermi('admin:banner:edit')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改轮播图", httpMethod = "PUT", notes = "修改轮播图")
    public AjaxResult edit(@RequestBody Banner banner) {
        return toAjax(bannerService.updateBanner(banner));
    }

    /**
     * 删除轮播图
     */
    @PreAuthorize("@ss.hasPermi('admin:banner:remove')")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除轮播图", httpMethod = "DELETE", notes = "删除轮播图")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(bannerService.deleteBannerByIds(ids));
    }

}
